package com.renshine.business.web.apps.module.screen.${module};

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.dataresolver.Param;
import com.alibaba.fastjson.JSONObject;
import com.renshine.business.repository.entity.${module}.${Entity};
import com.renshine.business.server.service.${module}.${Entity}Service;
import com.renshine.business.web.base.AbstractController;
import com.renshine.business.web.util.ParamsUtil;

/**
 * ${tb.comment}信息管理
 * 
 * @author ${author}
 *
 */
public class ${Entity}List extends AbstractController{
	
	@Autowired
	${Entity}Service ${entity}Service; 
	
	public void doPerform() {}
	
	// 搜索${tb.comment}列表
	public void doSearch(@Param("pageIndex") int pageIndex, @Param("limit") int limit) {
		JSONObject json = new JSONObject();
		try {
			Map<String, Object> data = ${entity}Service.search(getParametersMap(), pageIndex, limit);
			json.putAll(data);
			writeJsonHttp(json, "", true);
		} catch (Exception e) {
			log.error("${tb.comment}查询异常~", e);
			writeJsonHttp(json, "服务器打了一个盹~", false);
		}
	}
	
	// 添加${tb.comment}
	public void doAdd() {
		JSONObject json = new JSONObject();
		try {
			${Entity} ${entity} = ParamsUtil.convert(request, ${Entity}.class);			
			${entity}Service.save(${entity});
			writeJsonHttp(json, "保存成功", true);
		} catch (Exception e) {
			log.error("${tb.comment}保存异常~", e);
			writeJsonHttp(json, "服务器打了一个盹~", false);
		}	
	}

	// 编辑${tb.comment}
	public void doEdit() {
		JSONObject json = new JSONObject();
		try {
			${Entity} ${entity} = ParamsUtil.convert(request, ${Entity}.class);			
			${entity}Service.modify(${entity});
			writeJsonHttp(json, "", true);
		} catch (Exception e) {
			log.error("${tb.comment}修改异常~", e);
			writeJsonHttp(json, "服务器打了一个盹~", false);
		}	
	}
	
	// 删除${tb.comment}
	public void doRemove(@Param("${tb.pk.javaName}")String ${tb.pk.javaName}) {
		JSONObject json = new JSONObject();
		try {
			${entity}Service.remove(${tb.pk.javaName});
			writeJsonHttp(json, "", true);
		} catch (Exception e) {
			log.error("${tb.comment}删除异常~", e);
			writeJsonHttp(json, "服务器打了一个盹~", false);
		}
	}
	
	// ${tb.comment}详情信息
	public void doDetail(@Param("${tb.pk.javaName}")${tb.pk.javaType} ${tb.pk.javaName}) {
		JSONObject json = new JSONObject();
		try {
			${Entity} ${entity} = ${entity}Service.detail(${tb.pk.javaName});
			json.put("${entity}", ${entity});
			writeJsonHttp(json, "", true);
		} catch (Exception e) {
			log.error("${tb.comment}详情异常~", e);
			writeJsonHttp(json, "服务器打了一个盹~", false);
		}
	}
}
